//
//  VIPLatestCell.swift
//  GhostWorld
//
//  Created by Kam on 2018/5/17.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit
import AlamofireImage

class VIPLatestCell: UITableViewCell {

  // MARK: - IBOutlets
  
  @IBOutlet private weak var nameLabel: UILabel!
  @IBOutlet private weak var writerNameLabel: UILabel!
  @IBOutlet private weak var readerNameLabel: UILabel!
  @IBOutlet private weak var descriptionLabel: UILabel!
  @IBOutlet private weak var coverView: UIView!
  @IBOutlet private weak var coverImageView: UIImageView!
  
  // MARK: - Private vars
  
  private let selectView = UIView()
  
  // MARK: - Private methods
  
  
  
  // MARK: - Public vars
  
  
  
  // MARK: - Lifecycle
  
  override func awakeFromNib() {
    super.awakeFromNib()
    
    coverView.layer.masksToBounds = false
    coverView.layer.shadowColor = UIColor.black.cgColor
    coverView.layer.shadowOpacity = 0.4
    coverView.layer.shadowOffset = CGSize(width: 0, height: 1)
    coverView.layer.shadowRadius = 5
    coverView.layer.shouldRasterize = true
    
    coverImageView.layer.cornerRadius = 5
    coverImageView.backgroundColor = UIColor(fromRGB: 0x525252)
    
    // selected background view
    let backgroundView = UIView()
    backgroundView.backgroundColor = .clear
    
    selectView.backgroundColor = UIColor(fromRGB: 0x98000C)
    selectView.clipsToBounds = true
    selectView.layer.cornerRadius = 5
    backgroundView.addSubview(selectView)
    selectedBackgroundView = backgroundView
  }
  
  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    
    // Configure the view for the selected state
  }
  
  // MARK: - IBActions
  
  
  
  // MARK: - Public methods
  
  func prepare(album: Album) {
    nameLabel.text = album.name
    
    if let count = album.writers?.count, count > 0 {
      writerNameLabel.text = album.writers!.map{ ($0 as! Writer).name! }.joined(separator: ",")
    } else {
      writerNameLabel.text = "未知"
    }
    
    if let count = album.readers?.count, count > 0 {
      readerNameLabel.text = album.readers!.map{ ($0 as! Reader).name! }.joined(separator: ",")
    } else {
      readerNameLabel.text = "未知"
    }
    
    descriptionLabel.text = album.getShortDescription(length: 20)
    if let url = album.cover.getURL(size: .thumb) {
      coverImageView.af_setImage(withURL: url)
    }
  }

}

extension VIPLatestCell {
  static let defaultHeight: CGFloat = 150
}
